package cn.com.czpb.client.module.livenew;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.litepal.LitePal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.live.LiveListEntity;
import cn.com.czpb.client.model.livenew.LiveNewListBean;
import cn.com.czpb.client.model.livenew.LiveNewListEntity;
import cn.com.czpb.client.model.livenew.LiveTypeBean;
import cn.com.czpb.client.module.livenew.adapter.LiveListAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.ParamUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/6/26.
 */

public class LiveNewFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, OnRefreshListener,OnLoadmoreListener{
    @BindView(R.id.refresh_id)
    SmartRefreshLayout refresh_id;
    @BindView(R.id.recycler_id)
    RecyclerView recycler_id;

    @BindView(R.id.rl_live)
    LinearLayout rl_live;
    @BindView(R.id.ll_zhongyang)
    LinearLayout ll_zhongyang;
    @BindView(R.id.ll_bendi)
    LinearLayout ll_bendi;
    @BindView(R.id.ll_jingxuan)
    LinearLayout ll_jingxuan;
    @BindView(R.id.ll_collection)
    LinearLayout ll_collection;

    @BindView(R.id.tv_zhongyang)
    TextView tv_zhongyang;
    @BindView(R.id.tv_bendi)
    TextView tv_bendi;
    @BindView(R.id.tv_jingxuan)
    TextView tv_jingxuan;
    @BindView(R.id.tv_collection)
    TextView tv_collection;


    LiveListAdapter mAdapter;


    int Page = 1;
    int PageSize = 15;
    boolean isRefresh = false;
    String typeId;
    String myflag="0";;
    List<LiveTypeBean> typeBeanList;

    public static LiveNewFragment newInstance() {
        LiveNewFragment fragment = new LiveNewFragment();
        return fragment;
    }


    @Override
    public int setMyContentView() {
        return R.layout.fragment_live_new;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {



        ll_zhongyang.setSelected(true);
        typeBeanList = LitePal.findAll(LiveTypeBean.class);
        if (typeBeanList.size()>0){
            tv_zhongyang.setText(typeBeanList.get(0).getName());
            tv_bendi.setText(typeBeanList.get(1).getName());
            tv_jingxuan.setText(typeBeanList.get(2).getName());
        }else {
            tv_zhongyang.setText("中央新闻直播");
            tv_bendi.setText("本地新闻直播");
            tv_jingxuan.setText("精选推荐");
        }
        LogUtils.i("size---" + typeBeanList.size());

        tv_collection.setText("我的收藏");


        LayoutInflater inflater = LayoutInflater.from(activity);
        int size = typeBeanList.size();
//        for (int i = 0; i <size ; i++) {
//            View view=inflater.inflate(R.layout.ll_item_add,null);
//            TextView textView=view.findViewById(R.id.tv_name);
//            textView.setText(list.get(i).getName());
//        }

        initAdapter();
    }

    @OnClick({R.id.ll_zhongyang,R.id.ll_bendi,R.id.ll_jingxuan,R.id.ll_collection})
    public void OnClick(View view) {
        ll_zhongyang.setSelected(false);
        ll_bendi.setSelected(false);
        ll_jingxuan.setSelected(false);
        ll_collection.setSelected(false);




        switch (view.getId()) {
            case R.id.ll_zhongyang:
                typeId = typeBeanList.get(0).getTid();
                ll_zhongyang.setSelected(true);
                myflag="0";
                break;
            case R.id.ll_bendi:
                ll_bendi.setSelected(true);
                typeId = typeBeanList.get(1).getTid();
//                typeId = "2";
                myflag="0";
                break;
            case R.id.ll_jingxuan:
                ll_jingxuan.setSelected(true);
                typeId = typeBeanList.get(2).getTid();

//                typeId = "3";
                myflag="0";
                break;
            case R.id.ll_collection:
                ll_collection.setSelected(true);
                typeId="";
                myflag="1";
                break;
        }
        initList(typeId,true);
    }

    private void initAdapter() {
        mAdapter = new LiveListAdapter(null);
        refresh_id.setRefreshHeader(new ClassHeader_other(activity));
        GridLayoutManager manager = new GridLayoutManager(activity, 3);
        recycler_id.setLayoutManager(manager);
        recycler_id.setAdapter(mAdapter);
        refresh_id.setOnRefreshListener(this);
        refresh_id.setOnLoadmoreListener(this);
        mAdapter.setOnItemChildClickListener(this);
        initList("1",false);
    }

    private void initList(String typeid, final boolean cleared) {
        Map<String, String> map = new HashMap<>();
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("Page", Page + "");
        map.put("PageSize", PageSize + "");
        map.put("typeId", typeid);
        map.put("device", DeviceUtils.getMyUUID(activity));
        map.put("myflag", myflag);


        Factory.provideHttpService()
                .getliveList(ParamUtils.getRequestParam(map))
                .subscribeOn(Schedulers.io())
                .filter(new Func1<LiveNewListEntity, Boolean>() {
                    @Override
                    public Boolean call(LiveNewListEntity entity) {
                        LogUtils.e("code--" + entity.code);
                        return "200".equals(entity.code);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<LiveNewListEntity>() {
                    @Override
                    public void call(LiveNewListEntity entity) {
                        setList(entity.data,cleared);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable-->" + throwable.toString());
                    }
                });
    }

    private void setList(List<LiveNewListBean> list,boolean cleared) {
        if (cleared){
            mAdapter.setNewData(list);
        }else{
            if (isRefresh) {
                mAdapter.setNewData(list);
                mAdapter.notifyDataSetChanged();
            } else {
                mAdapter.addData(list);
                mAdapter.notifyDataSetChanged();
            }
        }

    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        Page++;
        initList(typeId,false);
        refresh_id.setLoadmoreFinished(true);
        refresh_id.finishLoadmore();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                isRefresh = true;
                Page = 1;
                initList(typeId,false);
            }
        },2000);
        refresh_id.setLoadmoreFinished(false);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        PageCtrl.start2PlayerLiveActivity(activity);
    }
}
